<?php
namespace app\common\model;

use think\Exception;

class RoomGoods extends Common
{
    protected $autoWriteTimestamp = true;
    protected $createTime = 'ctime';
    /**
     * 添加、移除宝贝
     * @param $data
     */
    public function doneGoods($data){
        try{
            $goods=$this->where(['uid'=>$data['uid'],'g_id'=>$data['id']])->find();
            if($goods){
                $res=$goods->delete();
            }else{
                $room_id=Room::where('uid',$data['uid'])->value('id');
                $res=$this->create([
                    'room_id'=>$room_id?$room_id:0,
                    'g_id'=>$data['id'],
                    'uid'=>$data['uid'],
                    'status'=>1,
                    'ctime'=>time()
                ]);
            }
            if($res){
                return [true,'操作成功'];
            }else{
                return [false,'操作失败'];
            }
        }catch (Exception $e){
            return [false,$e->getMessage()];
        }
    }
    /**
     * 获取可推广商品列表
     */
    public function spreadGoods($data){
        switch ($data['type']){
            case 1:#我的店铺商品
                $maps[]=[
                    ['a.uid','=',$data['uid']],
                    ['a.marketable','=',1]
                ];
                if(!empty($data['key'])){
                    $maps[]=['a.name|a.brief','like','%'.$data['key'].'%'];
                }
                $list=Goods::alias('a')->join('zp_room_goods g','g.g_id=a.id','left')
                    ->field('a.id,a.name,a.brief,a.price,a.image_id,a.fee,g.room_id')->where($maps);
                $list=$list->page($data['page'],$data['limit'])->select();
                $total=$list->count();
                foreach ($list as $k=>$v){
                    $list[$k]['is_add']=0;
                    $list[$k]['thumb']=_sImage($v['image_id']);
                    if(!empty($v['room_id'])){
                        $list[$k]['is_add']=1;
                    }
                }
                return ['count'=>$total,'list'=>$list];
                break;
            case 2:#精选联盟
                $maps[]=['marketable','=',1];
                if(!empty($data['key'])){
                    $maps[]=['name|brief','like','%'.$data['key'].'%'];
                }
                $list=Goods::where($maps)->field('id,name,brief,price,fee,image_id');
                $list=$list->page($data['page'],$data['limit'])->select();
                $total=$list->count();
                foreach ($list as $k=>$v){
                    $list[$k]['is_add']=0;
                    $list[$k]['thumb']=_sImage($v['image_id']);
                    $room_id=RoomGoods::where(['uid'=>$data['uid'],'g_id'=>$v['id']])->find();
                    if(!empty($room_id)){
                        $list[$k]['is_add']=1;
                    }
                }
                return ['count'=>$total,'list'=>$list];
                break;
            case 3:#最新添加
                $maps[]=['g.uid','=',$data['uid']];
                if(!empty($data['key'])){
                    $maps[]=['a.name|a.brief','like','%'.$data['key'].'%'];
                }
                $list=Goods::alias('a')->join('zp_room_goods g','g.g_id=a.id')->field('a.id,a.name,a.brief,a.price,a.fee,a.image_id')->where($maps);
                $list=$list->page($data['page'],$data['limit'])->select();
                $total=$list->count();
                foreach ($list as $k=>$v){
                    $list[$k]['is_add']=1;
                    $list[$k]['thumb']=_sImage($v['image_id']);
                }
                return ['count'=>$total,'list'=>$list];
                break;
        }
    }
}
